package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.exception.CustomerException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service


public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;

    @Override
    public void move(Long id)  {
        //查看菜品是否有关联数据
        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Dish::getCategoryId, id);

        int count1 = dishService.count(queryWrapper1);
        if(count1>0){
            throw new CustomerException("分类下存在菜品信息");
        }


        //查看套餐是否有关联数据
        //查看菜品是否有关联数据
        LambdaQueryWrapper<Setmeal> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(Setmeal::getCategoryId, id);

       // Wrappers.lambdaQuery(Dish.class).eq(Dish::getCategoryId, id);


        int count2 = setmealService.count(queryWrapper2);
        // int count2 = SetmealService.count(queryWrapper2);
        if(count2>0){
            throw new CustomerException("分类下存在套餐信息");
        }

        //删除空的分类信息
        this.removeById(id);
    }

    @Override
    public Page<Category> page(Integer page, Integer pageSize) {

        Page<Category> pageInfo = new Page(page, pageSize);

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();


        //指定查询规则,会动态拼接到sql语句上
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        this.page(pageInfo, queryWrapper);

        return pageInfo;
    }
}
